package com.example.hm_dtfkz.util;

import android.content.Context;
import android.util.Log;

import com.tencent.mmkv.MMKV;

/**
 * FileName: MMKV
 * Author: admin
 * Date: 2021/10/12 15:37
 * Description:
 */
public class Mmkv {

    private static volatile Mmkv instance = null;
    private static MMKV mmkv;

    public static Mmkv getInstance() {
        if (instance == null) {
            synchronized (Mmkv.class) {
                if (instance == null) {
                    instance = new Mmkv();
                }
            }
        }
        return instance;
    }

    public void init(Context context) {
        String dir = context.getFilesDir().getAbsolutePath() + "/mmkv";
        String rootDir = MMKV.initialize(dir);
        Log.e("MMKV", "mmkv root: " + rootDir);
        mmkv = MMKV.defaultMMKV();
    }

    /**
     * 写入字符串型数据
     */
    public static void setString(String key, String value) {
        if (mmkv != null) {
            mmkv.encode(key, value);
        }
    }

    /**
     * 获取字符串型数据
     */
    public static String getString(String key) {
        if (mmkv != null) {
            return mmkv.decodeString(key);
        }
        return null;
    }

    /**
     * 写入整型数据
     */
    public static void setInteger(String key, int value) {
        if (mmkv != null) {
            mmkv.encode(key, value);
        }
    }

    /**
     * 获取整型数据
     */
    public static Integer getInteger(String key) {
        if (mmkv != null) {
            return mmkv.decodeInt(key, 0);
        }
        return 0;
    }

    /**
     * 获取整型数据
     */
    public static Integer getInteger(String key, int defaultValue) {
        if (mmkv != null) {
            return mmkv.decodeInt(key, defaultValue);
        }
        return 0;
    }

    /**
     * 写入布尔型数据
     */
    public static void setBoolean(String key, boolean value) {
        if (mmkv != null) {
            mmkv.encode(key, value);
        }
    }

    /**
     * 获取布尔型数据
     */
    public static boolean getBoolean(String key) {
        if (mmkv != null) {
            return mmkv.decodeBool(key, false);
        }
        return false;
    }

    /**
     * 写入长整型数据
     */
    public static void setLong(String key, long value) {
        if (mmkv != null) {
            mmkv.encode(key, value);
        }
    }

    /**
     * 获取长整型数据
     */
    public static long getLong(String key) {
        if (mmkv != null) {
            return mmkv.decodeLong(key, 0);
        }
        return 0;
    }

    /**
     * 写入浮点型数据
     */
    public static void setDouble(String key, double value) {
        if (mmkv != null) {
            mmkv.encode(key, value);
        }
    }

    /**
     * 获取浮点型数据
     */
    public static double getDouble(String key) {
        if (mmkv != null) {
            return mmkv.decodeDouble(key, 0.0);
        }
        return 0.0;
    }

    public static boolean contains(String key) {
        if (mmkv != null) {
            return mmkv.contains(key);
        }
        return false;
    }

    public static void removeKey(String key) {
        if (mmkv != null) {
            if (mmkv.contains(key)) {
                mmkv.removeValueForKey(key);
            }
        }
    }
}